﻿using Kaysite.Model;

public partial class Controls_Content_Title : BaseControl
{
    protected override void DoInitialize_2_LoadDataInitControls()
    {
        base.DoInitialize_2_LoadDataInitControls();
        lblTitle.Text = GetElement(Request.Path, CurrentPage.CurrentSitePage.GetName());
        lblTitle.Text += GetCrumbs(CurrentView);
    }

    private string GetCrumbs(string view)
    {
        var result = string.Empty;
        var element = CurrentPage.Breadcrumbs.Find(b => b.View == view);
        if (element != null)
        {
            if (element.Type != eCrumbType.Hidden)
                result = string.Format(" / {0}", GetElement(Request.Path, element.Title, element.View, element.Type));
            if (!string.IsNullOrEmpty(element.ParentView))
                result = GetCrumbs(element.ParentView) + result;
        }
        return result;
    }

    private string GetElement(string page, string title, string view = null, eCrumbType type = eCrumbType.Link)
    {
        var href = SitePath + page;
        if (!string.IsNullOrEmpty(view))
            href += "?view=" + view;
        switch (type)
        {
            case eCrumbType.Link:
                return string.Format("<a href=\'{0}\' {2}>{1}</a>",
                    href,
                    title,
                    CurrentView == view ? "class='active'" : string.Empty);
            case eCrumbType.Text:
                return string.Format("<span {0}>{1}</a>",
                    CurrentView == view ? "class='active'" : string.Empty,
                    title);
        }
        return string.Empty;
    }

    public void SetTitle(string message)
    {
        lblTitle.Text += string.Format(" / <span class='active'>{0}</span>", message);
    }
}